package edu.whu.puppygo.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Getter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class OssConfig {
    @Getter
    private final static int fileNameMaxLength = 100;
    private static String accessKeyId = "LTAI5tErUVJuaCagU3CHYo4t";
    private static String accessKeySecret = "nT0ZFRjvfuPx6uJcVnwDdrwgYbC6SU";
    @Getter
    private static String fullUrl = "http://puppygo.oss-rg-china-mainland.aliyuncs.com/";

    @Getter
    private static String picPath = "pic/";

    @Getter
    private static String petPicPath = "petPic/";

    @Getter
    private static String fragmentPic = "fragmentPic/";

    @Getter
    private static String growthRecordPic = "avatarPic/";

    @Getter
    private static String bucketName = "puppygo";

    @Getter
    private static String endpoint = "oss-rg-china-mainland.aliyuncs.com";

    private volatile static OSS ossClient;
    private volatile static OSSClientBuilder ossClientBuilder;

    @Bean
    @Scope("prototype")
    public static OSS initOSSClient() {
        if (ossClient == null) {
            synchronized (OssConfig.class) {
                if (ossClient == null) {
                    ossClient = initOSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                }
            }
        }
        return ossClient;
    }

    public static OSSClientBuilder initOSSClientBuilder() {
        if (ossClientBuilder == null) {
            synchronized (OssConfig.class) {
                if (ossClientBuilder == null) {
                    ossClientBuilder = new OSSClientBuilder();
                }
            }
        }
        return ossClientBuilder;
    }
}
